/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datasource;

import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
//import java.util.concurrent.LinkedTransferQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Carlos Eduardo Bernal Cárdenas
 * @version 1.0
 * @created 20-ago-2011
 */
public class BMPImage {

    private String pathImage;
    /**
     * Header 14 data
     */
    //2 data
    private String signature;
    //4 data
    private int fileSize;
    //4 data
    private int reserved;
    //4 data
    private int dataOffset;
    /**
     * InfoHeader 40 data
     */
    //4 data
    private int sizeInfoHeader = 40;
    //4 data
    private int width;
    //4 data
    private int height;
    //2 data
    private int planes;
    //2 data
    /**
     * 1 = monocormatic
     * 4 = 4bit palletized
     * 8 = 8bit palletized
     * 16 = 16bit RGB
     * 24 = 24 bit RGB
     */
    private int bitCount;
    //4 data
    private int compression;
    //4 data
    private int imageSize;
    //4 data
    private int xPixelsPerM;
    //4 data
    private int yPixelsPerM;
    //4 data
    private int colorsUsed;
    //4 data
    private int colorsImportant;
    private boolean exist = true;
    private ArrayList<ArrayList<Color>> dataImage = new ArrayList<ArrayList<Color>>();

    public BMPImage(String pathImage) {
        this.pathImage = pathImage;
        loadImageProperties();
        for (int i = dataImage.size() - 1; i >= 0; i--) {
            for (int j = 0; j < dataImage.get(i).size(); j++) {
                System.out.print(dataImage.get(i).get(j) + " - ");
            }
            System.out.println("");
        }
    }

    private void loadImageProperties() {
        try {
            /**** Load Image Properties****/
            InputStream is = new FileInputStream(pathImage);
            byte[] header = new byte[54];
            is.read(header, 0, 54);
//            int a = readBytes(1, header, 0);
//            int b = readBytes(1, header, 1);
//            System.out.println(a + " - " + b);
//            System.out.println(((char) a) + " - " + ((char) b));
            //2 data
            signature = ((char) (readBytes(1, header, 0))) + "" + ((char) (readBytes(1, header, 1)));
            //4 data
            fileSize = readBytes(4, header, 2);
            //4 data
            reserved = readBytes(4, header, 6);
            //4 data
            dataOffset = readBytes(4, header, 10);
            /**
             * InfoHeader 40 data
             */
            //4 data
            sizeInfoHeader = 40;
            //4 data
            width = readBytes(4, header, 18);
            //4 data
            height = readBytes(4, header, 22);
            //2 data
            planes = readBytes(2, header, 26);
            //2 data
            /**
             * 1 = monocormatic
             * 4 = 4bit palletized
             * 8 = 8bit palletized
             * 16 = 16bit RGB
             * 24 = 24 bit RGB
             */
            bitCount = readBytes(2, header, 28);
            //4 data
            compression = readBytes(4, header, 30);
            //4 data
            /**
             * compressed
             */
            imageSize = readBytes(4, header, 34);
            //4 data
            xPixelsPerM = readBytes(4, header, 38);
            //4 data
            yPixelsPerM = readBytes(4, header, 42);
            //4 data
            colorsUsed = readBytes(4, header, 46);
            //4 data
            colorsImportant = readBytes(4, header, 50);

            /**** Load Image****/
            is = new FileInputStream(pathImage);
            byte[] data = new byte[fileSize];
            is.read(data, 0, fileSize);
            ArrayList<Color> pixels = new ArrayList<Color>();
            for (int i = 54; i < data.length - 3; i += 3) {
                int b = (data[i] + 256) % 256;
                int g = (data[i + 1] + 256) % 256;
                int r = (data[i + 2] + 256) % 256;
                //System.out.print(r + " " + g + " " + b + "*");
                Color c = new Color(r, g, b);
                pixels.add(c);
                if ((i + 53) % width == 0) {
                    //System.out.println("");
                    dataImage.add(pixels);
                    pixels = new ArrayList<Color>();
                }
            }
            is.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BMPImage.class.getName()).log(Level.SEVERE, null, ex);
            exist = false;
        } catch (IOException ex) {
            Logger.getLogger(BMPImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private int readBytes(int data, byte[] arrBytes, int from) {
        int temp = 0;
        for (int i = from; i < from + data; i++) {
            temp += arrBytes[i] * ((int) Math.pow(256, i - from));
        }
        return temp;
    }

    private void loadImage() {
        if (exist) {
        }
    }
}
